
<template>
  <div>
    <el-card>
      <el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="课程Id" prop="courseId">
          <el-input v-model="ruleForm.courseId" :value="ruleForm.courseId"></el-input>
        </el-form-item>
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="ruleForm.courseName" :value="ruleForm.courseName"></el-input>
        </el-form-item>
        <el-form-item label="学生Id" prop="studentId">
          <el-input v-model="ruleForm.studentId" :value="ruleForm.studentId"></el-input>
        </el-form-item>
        <el-form-item label="学生名" prop="studentName">
          <el-input v-model="ruleForm.studentName" :value="ruleForm.studentName"></el-input>
        </el-form-item>
        <el-form-item label="分数" prop="score">
          <el-input v-model.number="ruleForm.score" :value="ruleForm.score"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
      courseId: '',
      courseName: '',
      score: 0,
      studentId: '',
      studentName: '',
      teacherId: '',
      teacherName: ''
      },
      rules: {
      courseId: [{ required: true, message: "请填写课程编号" },],
      courseName: [{ required: true, message: "请填写课程名称" }],
      studentId: [{ required: true, message: "请填写学生Id" }],
      studentName: [{ required: true, message: "请填写学生名称" }],
      score: [{ required: true, message: "请填写成绩" }],
      // Remove the previous student-related validation rules
        
      }
    };
  },
  created() {
    if(localStorage.getItem("type") !== 'teacher'){this.$router.push(`/login`);this.$message.error('你不是老师');}
    this.ruleForm.teacherName = localStorage.getItem('name');
    this.ruleForm.teacherId = localStorage.getItem('teacherId');
    this.pageForm.teacherId = localStorage.getItem('teacherId');//要有teacherId
  },
  methods: {
  // 修改学生成绩信息
async insertGradeInfo() {
  // JSON化
  const config = {
    headers: {
      'Content-Type': 'application/json',
      'token':`${localStorage.getItem("token")}`
    }
  };
  try {
    const { data: res } = await this.$http.post(`api/admin/grade/insert`, JSON.stringify(this.ruleForm), config);
    if (res.status !== 500) {
      this.$message.success('成功添加学生成绩');
      // Add code to update the Grade table if necessary
    } else {
      this.$message.error('添加成绩失败');
    }
  } catch (error) {
    // Handle request error
    console.error('Request Error:', error);
    this.$message.error('添加成绩失败');
  }
},

// 提交表单信息
submitForm(ruleForm) {
  this.$refs[ruleForm].validate((valid) => {
    if (valid) {
      this.insertGradeInfo();
    } else {
      this.$message.error('请将信息填写完整');
    }
  });
},

// 重置清空表单信息
resetForm(formName) {
  this.$refs[formName].resetFields();
},
  }
}

</script>